const { mysql } = require('../../mysql')

async function submitAction (ctx) {
    const { openId } = ctx.request.body
    let goodsId = ctx.request.body.goodsId
    let allPrice = ctx.request.body.allPrice

    // 查询是否存在订单
    const isOrder = await mysql('nideshop_order').where({
        'user_id': openId
    }).select()
    // 如果原本有订单则更新数据
    if (isOrder.length > 0) {
        const data = await mysql('nideshop_order').where({
          'user_id': openId
        }).update({
          user_id: openId,
          goods_id: goodsId,
          allprice: allPrice
        })
        // 往前端输出更新成功或失败
        if (data) {
            ctx.body = {
              data: true
            }
        } else {
            ctx.body = {
              data: false
            }
        }
        // 如果没有则直接插入数据
    } else {
        const data = await mysql('nideshop_order').insert({
            user_id: openId,
            goods_id: goodsId,
            allprice: allPrice
        })
        if (data) {
            ctx.body = {
            data: true
            }
        } else {
            ctx.body = {
            data: false
            }
        }
    }
}

async function detailAction (ctx) {
    const openId = ctx.query.openId
    const addressId = ctx.query.addressId || ''
    // 订单数据
    const orderDetail = await mysql('nideshop_order').where({
        'user_id': openId
    }).select()
    var goodsIds = orderDetail[0].goods_id.split(',')
    // console.log(goodsIds)

    // 购物车列表数据
    const list = await mysql('nideshop_cart').andWhere({
        'user_id': openId
    }).whereIn('goods_id', goodsIds).select();
    // console.log(list)

    // 收货地址
    var addressList;
    if (addressId) {
        addressList = await mysql('nideshop_address').where({
        'user_id': openId,
        'id': addressId
        }).orderBy('is_default', 'desc').select()
    } else {
        addressList = await mysql('nideshop_address').where({
        'user_id': openId
        }).orderBy('is_default', 'desc').select()
    }
    ctx.body = {
        price: orderDetail[0].allprice,
        goodsList: list,
        address: addressList[0] || {}
    }
}

module.exports = {
    submitAction,
    detailAction
}